<?php
namespace app\huaban\controller;
use think\Db;
use think\Request;
use think\Controller;
use think\Session;
use think\Cookie;
use think\image;
class Index
{
    public function index()
    {
        $session = session::get();
//        var_dump($session);
        if(!empty($session['huaban_user_name'])){
            $data = Db::name('huaban_user')->where('name',$session['huaban_user_name'])->find();
            if(!empty($data)){
//
                return view('/index/indexhave',[
                    'data'=>$data,
                ]);
            }


        }else{

            return view('/index/index');

        }

    }

    public function reg(Request $request)
    {
        $data = input('post.');

        unset($data['regRePass']);
        $data['pass']= password_hash($data['pass'],1);
        $result = Db::name('huaban_user')->insertGetId($data); // 自增ID

        $list = [
            'integral'=>20,
            'uid'=>$result,
        ];
        $res = Db::name('huaban_integral')->data($list)->insert();


        return $res;
    }

    public function login(Request $request)
    {
        $p = input('post.');
        $data = Db::name('huaban_user')->where('name',$p['name'])->find();
        if(!password_verify($p['pass'],$data['pass'])){
            return $this->error( '密码错误');
        }

        if($p['check']==2){
            $result = Db::table('xy_huaban_user')->where(['name'=>$data['name']])->update(['check'=>2]);

            cookie::set('huaban_user_name',$p['name']);
            cookie::set('huaban_user_pass',$p['pass']);
            cookie::set('huaban_user_check',$p['check']);
            cookie::set('huaban_user_id',$data['id']);
//                var_dump($result);
        }else{
            $result = Db::table('xy_huaban_user')->where(['name'=>$data['name']])->update(['check'=>1]);
//                var_dump($result);
        }


        session::set('huaban_user_name',$p['name']);
        session::set('huaban_user_pass',$p['pass']);
        session::set('huaban_user_check',$p['check']);
        session::set('huaban_user_id',$data['id']);






        return json($data);
    }




}
